package com.eolwral.osmonitor.messages;

import java.util.Locale;

import com.eolwral.osmonitor.OSMonitorService;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;

import com.eolwral.osmonitor.*;
import com.eolwral.osmonitor.preferences.Preferences;

public class DmesgBox extends DebugBox
{
	@Override
	public void onResume()
	{
		restorePrefs();
		JNILibrary.TruncateDebugMessage();
		JNILibrary.doTaskStart(JNILibrary.doTaskDMesg);

		super.onResume();
	}
	
	@Override
	protected int getMessageCount()
	{
		return JNILibrary.GetDebugMessageCounts();
	}
	
	@Override
	protected String getLogMessage(int index, boolean html)
	{
		if (html)
			return String.format(Locale.US,
				"<tr><td>%s</td><td>[%s]</td><td>%s</td></tr>\n",
				JNILibrary.GetDebugMessageTime(index),
				JNILibrary.GetDebugMessageLevel(index),
				JNILibrary.GetDebugMessage(index));
		else
			return String.format(Locale.US, "%s [%s] %s\n",
				JNILibrary.GetDebugMessageTime(index),
				JNILibrary.GetDebugMessageLevel(index),
				JNILibrary.GetDebugMessage(index));
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		TabIndex = 6;
		
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void restorePrefs()
	{
		SharedPreferences settings = PreferenceManager
				.getDefaultSharedPreferences(this);

		InternalList = (ListView) findViewById(R.id.debuglist);
		InternalList.setEmptyView(findViewById(R.id.debugempty));
		InternalList.setAdapter(new DMesgListAdapter(this));
		UpdateInterface = (DMesgListAdapter) InternalList.getAdapter();

		if (settings.getBoolean(Preferences.PREF_DMESGUSEFILTER, false))
		{
			JNILibrary.SetDebugMessageFilter(1);
			JNILibrary.SetDebugMessage(settings.getString(
					Preferences.PREF_DMESGFILTERSTR, ""));
			JNILibrary.SetDebugMessageLevel(Integer.parseInt(settings
					.getString(Preferences.PREF_DMESGFILTERLV, "8")));
		} else
			JNILibrary.SetDebugMessageFilter(0);

		if (settings.getBoolean(Preferences.PREF_STATUSBAR, false))
		{
			if (OSMonitorService.getInstance() == null)
				startService(new Intent(this, OSMonitorService.class));
			else
				OSMonitorService.getInstance().Notify();
		} else if (OSMonitorService.getInstance() != null)
			OSMonitorService.getInstance().stopSelf();
	}

	private class DMesgListAdapter extends BaseAdapter
	{
		public DMesgListAdapter(Context context)
		{
			mContext = context;
		}

		public int getCount()
		{
			return JNILibrary.GetDebugMessageCounts();
		}

		public Object getItem(int position)
		{
			return position;
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			DMesgDetailView sv;

			if (convertView == null)
			{
				sv = new DMesgDetailView(mContext,
						JNILibrary.GetDebugMessageTime(position),
						JNILibrary.GetDebugMessageLevel(position),
						JNILibrary.GetDebugMessage(position), position);
			}
			else
			{
				sv = (DMesgDetailView) convertView;
				sv.setContext(JNILibrary.GetDebugMessageTime(position),
						JNILibrary.GetDebugMessageLevel(position),
						JNILibrary.GetDebugMessage(position), position);
			}

			return sv;
		}

		private Context mContext;
	}

	private class DMesgDetailView extends TableLayout
	{
		private TextView DMesgLevel;
		private TextView DMesgMsg;

		public DMesgDetailView(Context context, String Time, String Level,
				String Msg, int position)
		{
			super(context);

			DMesgMsg = new TextView(context);
			DMesgLevel = new TextView(context);

			DMesgLevel.setGravity(Gravity.LEFT);
			DMesgLevel.setPadding(3, 3, 3, 3);
			DMesgLevel.setWidth(60);

			DMesgMsg.setGravity(Gravity.LEFT);
			DMesgMsg.setPadding(3, 3, 3, 3);
			DMesgMsg.setWidth(getWidth() - 60);

			DMesgLevel.setText(Time + " [" + Level + "]");

			if (Level.endsWith("EMERGENCY"))
				DMesgLevel.setTextColor(Color.RED);
			else if (Level.endsWith("ALERT"))
				DMesgLevel.setTextColor(Color.RED);
			else if (Level.endsWith("CRITICAL"))
				DMesgLevel.setTextColor(Color.RED);
			else if (Level.endsWith("ERROR"))
				DMesgLevel.setTextColor(Color.RED);
			else if (Level.endsWith("WARNING"))
				DMesgLevel.setTextColor(Color.YELLOW);
			else if (Level.endsWith("NOTICE"))
				DMesgLevel.setTextColor(Color.MAGENTA);
			else if (Level.endsWith("INFORMATION"))
				DMesgLevel.setTextColor(Color.GREEN);
			else if (Level.endsWith("DEBUG"))
				DMesgLevel.setTextColor(Color.BLUE);

			DMesgMsg.setText(Msg);

			addView(DMesgLevel);
			addView(DMesgMsg);

			if (position % 2 == 0)
				setBackgroundColor(0x80444444);
			else
				setBackgroundColor(0x80000000);

		}

		public void setContext(String Time, String Level, String Msg,
				int position)
		{
			DMesgLevel.setText(Time + " [" + Level + "]");

			if (Level.endsWith("EMERGENCY"))
				DMesgLevel.setTextColor(Color.RED);
			else if (Level.endsWith("ALERT"))
				DMesgLevel.setTextColor(Color.RED);
			else if (Level.endsWith("CRITICAL"))
				DMesgLevel.setTextColor(Color.RED);
			else if (Level.endsWith("ERROR"))
				DMesgLevel.setTextColor(Color.RED);
			else if (Level.endsWith("WARNING"))
				DMesgLevel.setTextColor(Color.YELLOW);
			else if (Level.endsWith("NOTICE"))
				DMesgLevel.setTextColor(Color.MAGENTA);
			else if (Level.endsWith("INFORMATION"))
				DMesgLevel.setTextColor(Color.GREEN);
			else if (Level.endsWith("DEBUG"))
				DMesgLevel.setTextColor(Color.BLUE);

			DMesgMsg.setText(Msg);

			if (position % 2 == 0)
				setBackgroundColor(0x80444444);
			else
				setBackgroundColor(0x80000000);

		}
	}
}
